<?php


define('CODE_SUCCESS', 1000);
define('CODE_ADDED', 1001);
define('CODE_EDITED', 1002);
define('CODE_DELETED', 1003);
define('CODE_ACCEPTED', 1004);

define('CODE_ERROR', 2000);
define('CODE_NOT_FOUND', 2001);
define('CODE_FORBIDDEN', 2002);


class AutomateMethodsComponent extends Object {

/******************************************************************************/

var $components = array(
	'Session',
	'RequestHandler',
	'Auth'
);

/******************************************************************************/

var $Controller = NULL;

var $settings = array();

/******************************************************************************/

var $autoLanguage = NULL;
var $autoPage = NULL;
var $autoPrefixes = NULL;
var $autoParams = NULL;
var $autoNamed = NULL;
var $autoExtensions = NULL;

/******************************************************************************/

var $pageDescription = '';
var $pageKeywords = '';
var $pageCopyright = '';
var $pageHeaderText = '';
var $pageFooterText = '';

/******************************************************************************/

function initialize(&$Controller, $settings = array()) {
	$ctrl = $this->Controller = &$Controller;
	$this->settings = (array)$settings;
	// debug
	$ctrl->set('debug', $debug = Configure::read('debug'));
	// project
	$ctrl->set('project_languages', $project_languages = Configure::read('Project.languages'));
	$ctrl->set('project_modules', $project_modules = Configure::read('Project.modules'));
	// admin
	$admin_route = 'admin'; // Configure::read('Routing.admin');
	if (isset($ctrl->params[$admin_route]) && (bool)$ctrl->params[$admin_route]) {
		Configure::write('admin', TRUE);
	} else {
		Configure::write('admin', FALSE);
	}
	$ctrl->set('admin', $admin = Configure::read('admin'));
	// preview
	if (isset($ctrl->params['preview']) && (bool)$ctrl->params['preview']) {
		Configure::write('preview', TRUE);
	} else {
		Configure::write('preview', FALSE);
	}
	$ctrl->set('preview', $preview = Configure::read('admin'));
// language
// removed code, see AppController::_setLanguage()
	// for admin
	if ($admin) {
		$ctrl->layout = 'admin';
		$ctrl->view = 'Admin.Automate';
		// request handler
		$this->RequestHandler->setContent('frame', 'text/html');
	}
	// auth
	$this->Auth->authError = __('auth_error_message', TRUE);
	$this->Auth->loginError = __('login_error_message', TRUE);
	$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'welcome', 'admin' => TRUE);
	// not for admin
	if (!$admin) {
		$this->Auth->allow('*');
	}
}

function startup(&$Controller) {
	$ctrl = &$this->Controller;
	// pageCaption
	$string_id = $ctrl->params['controller'] . '_caption';
	$controllerCaption = __($string_id, TRUE);
	if ($controllerCaption !== $string_id) {
		$string_id = $ctrl->params['action'] . '_caption';
		$actionCaption = __($string_id, TRUE);
		if ($actionCaption !== $string_id) {
			$ctrl->PageTitle->pageCaption = preg_replace(array('~\{action\}~i' /* <<< deprecated */ , '~\{action_caption\}~i'), array($actionCaption, $actionCaption), $controllerCaption);
			$ctrl->PageTitle->pageCaption = preg_replace('~\{caption\}~i', '{data_caption}', $ctrl->PageTitle->pageCaption); // deprecated / legacy fix
		}
	}
}

/******************************************************************************/

function disableAll() {
	$ctrl = &$this->Controller;
	$ctrl->autoRender = FALSE;
	Configure::write('debug', 0); // no debug
	$ctrl->disableCache();
}

function JSON($value, $exit = TRUE) {
	$this->disableAll();
	if (!headers_sent()) {
		header('Content-Type: application/json; charset=utf-8');
	}
	echo json_encode($value);
	if ($exit) {
		$this->Controller->_stop();
	}
}

function isAjax_JSON($value, $exit = TRUE) {
	if ($this->RequestHandler->isAjax()) {
		$this->JSON($value, $exit);
		return TRUE;
	}
	return FALSE;
}

/******************************************************************************/

function autoUrl($url = NULL) {
	$ctrl = &$this->Controller;
	if (is_null($url) || is_array($url)) {
		// autoLanguage
		$autoLanguage = (bool)Configure::read('Routing.autoLanguage');
		if (isset($url['autoLanguage'])) {
			$autoLanguage = (bool)$url['autoLanguage'];
			unset($url['autoLanguage']);
		} elseif (!is_null($this->autoLanguage)) {
			$autoLanguage = (bool)$this->autoLanguage;
		}
		if ($autoLanguage && !isset($url['language']) && isset($ctrl->params['language'])) {
			$url['language'] = $ctrl->params['language'];
		}
		// autoPage
		$autoPage = (bool)Configure::read('Routing.autoPage');
		if (isset($url['autoPage'])) {
			$autoPage = (bool)$url['autoPage'];
			unset($url['autoPage']);
		} elseif (!is_null($this->autoPage)) {
			$autoPage = (bool)$this->autoPage;
		}
		if ($autoPage && !isset($url['page']) && isset($ctrl->params['named']['page'])) {
			$url['page'] = $ctrl->params['named']['page'];
		}
		// autoPrefixes
		$autoPrefixes = (array)Configure::read('Routing.autoPrefixes');
		if (isset($url['autoPrefixes'])) {
			$autoPrefixes = array_merge($autoPrefixes, (array)$url['autoPrefixes']);
			unset($url['autoPrefixes']);
		} elseif (!is_null($this->autoPrefixes)) {
			$autoPrefixes = array_merge($autoPrefixes, (array)$this->autoPrefixes);
		}
		foreach ($autoPrefixes as $prefix) {
			if (!isset($url[$prefix]) && isset($ctrl->params[$prefix]) && (bool)$ctrl->params[$prefix]) {
				$url[$prefix] = TRUE;
			}
		}
		// autoParams
		$autoParams = (array)Configure::read('Routing.autoParams');
		if (isset($url['autoParams'])) {
			$autoParams = array_merge($autoParams, (array)$url['autoParams']);
			unset($url['autoParams']);
		} elseif (!is_null($this->autoParams)) {
			$autoParams = array_merge($autoParams, (array)$this->autoParams);
		}
		foreach ($autoParams as $arg) {
			if (!isset($url[$arg]) && isset($ctrl->params[$arg])) {
				$url[$arg] = $ctrl->params[$arg];
			}
		}
		// autoNamed
		$autoNamed = (array)Configure::read('Routing.autoNamed');
		if (isset($url['autoNamed'])) {
			$autoNamed = array_merge($autoNamed, (array)$url['autoNamed']);
			unset($url['autoNamed']);
		} elseif (!is_null($this->autoNamed)) {
			$autoNamed = array_merge($autoNamed, (array)$this->autoNamed);
		}
		foreach ($autoNamed as $arg) {
			if (!isset($url[$arg]) && isset($ctrl->params['named'][$arg])) {
				$url[$arg] = $ctrl->params['named'][$arg];
			}
		}
		// autoExtensions
		$autoExtensions = (array)Configure::read('Routing.autoExtensions');
		if (isset($url['autoExtensions'])) {
			$autoExtensions = array_merge($autoExtensions, (array)$url['autoExtensions']);
			unset($url['autoExtensions']);
		} elseif (!is_null($this->autoExtensions)) {
			$autoExtensions = array_merge($autoExtensions, (array)$this->autoExtensions);
		}
		foreach ($autoExtensions as $ext) {
			if (!isset($url['ext']) && isset($ctrl->params['url']['ext']) && ($ext === $ctrl->params['url']['ext'])) {
				$url['ext'] = $ctrl->params['url']['ext'];
			}
		}
	}
	return $url;
}

function url($url = NULL, $full = FALSE) {
	return Router::url($this->autoUrl($url), $full);
}

/******************************************************************************/

function beforeRedirect(&$Controller, $url, $status = NULL, $exit = TRUE) {
	//$this->isAjax_JSON(array('url' => $url, 'status' => $status), $exit);
	return $this->autoUrl($url);
}

/******************************************************************************/

function processMetaSEO($row = array(), $modelClass = 'Model') {
	$ctrl = &$this->Controller;
	if (isset($row[$ctrl->{$modelClass}->name]) && is_array($row[$ctrl->{$modelClass}->name])) {
		foreach ($row[$ctrl->{$modelClass}->name] as $field_name => $field_value) {
			if ((bool)preg_match('~^(meta|seo)_([a-z_]+)$~i', $field_name, $match) && !empty($field_value)) {
				switch ($match[2]) {
case 'title': $ctrl->PageTitle->pageTitle = $field_value; break;
case 'subtitle': $ctrl->PageTitle->pageSubtitle = $field_value; break;
					case 'description': $this->pageDescription = $field_value; break;
					case 'keywords': $this->pageKeywords = $field_value; break;
					case 'copyright': $this->pageCopyright = $field_value; break;
					case 'header_text': $this->pageHeaderText = $field_value; break;
					case 'footer_text': $this->pageFooterText = $field_value; break;
				}
			}
		}
	}
}

function _processMetaSEO($model_data = array()) { // deprecated
	$ctrl = &$this->Controller;
	if (is_array($model_data)) foreach ($model_data as $name => $value) {
if ((bool)preg_match('~^(?:meta|seo)_title$~i', $name) && !empty($value)) $ctrl->PageTitle->pageTitle = $value;
if ((bool)preg_match('~^(?:meta|seo)_subtitle$~i', $name) && !empty($value)) $ctrl->PageTitle->pageSubtitle = $value;
		if ((bool)preg_match('~^(?:meta|seo)_description$~i', $name) && !empty($value)) $this->pageDescription = $value;
		if ((bool)preg_match('~^(?:meta|seo)_keywords$~i', $name) && !empty($value)) $this->pageKeywords = $value;
		if ((bool)preg_match('~^(?:meta|seo)_copyright$~i', $name) && !empty($value)) $this->pageCopyright = $value;
		if ((bool)preg_match('~^(?:meta|seo)_header_text$~i', $name) && !empty($value)) $this->pageHeaderText = $value;
		if ((bool)preg_match('~^(?:meta|seo)_footer_text$~i', $name) && !empty($value)) $this->pageFooterText = $value;
	}
}

function setPageVars() {
	$ctrl = &$this->Controller;
	$ctrl->set('description_for_layout', h($this->pageDescription));
	$ctrl->set('keywords_for_layout', h($this->pageKeywords));
	$ctrl->set('copyright_for_layout', h($this->pageCopyright));
	$ctrl->set('header_text_for_layout', $this->pageHeaderText);
	$ctrl->set('footer_text_for_layout', $this->pageFooterText);
}

/******************************************************************************/

function beforeRender(&$Controller) {
	$this->setPageVars();
}

/******************************************************************************/

}


?>